﻿Public Class KidView
    Public eid As Integer
    Private Sub KidView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        'UI.SetPlaceHolder(TextBox1)
        loadchild()
    End Sub
    Private Sub loadchild()
        Dim ti = Now.Year
        'Dim dt As DataTable = cmd.ExcecuteDatatable("SELECT c.ChildId,c.ChildName,c.ChildDOB,c.SexId,s.SexName," & ti & "-year(c.ChildDOB) as Age FROM [Child] c INNER JOIN Sex s on s.SexId=c.SexId WHERE EmpId=" & eid & " AND active=true ")
        'DataGridView1.DataSource = cmd.ExcecuteDatatable("SELECT c.ChildId,c.ChildName,c.ChildDOB,c.SexId,s.SexName," & ti & "-year(c.ChildDOB) as Age FROM [Child] c INNER JOIN Sex s on s.SexId=c.SexId WHERE EmpId=" & eid & " AND active=true ")
        DataGridView1.DataSource = cmd.ExcecuteDatatable("SELECT r.RelateId,r.RelateName,r.RelateDOB,r.SexId,s.SexName," & ti & "-year(r.RelateDOB) as Age FROM [Relative] r INNER JOIN Sex s on s.SexId=r.SexId WHERE EmpId=" & eid & " AND active=true AND TypeId=2 ")
    End Sub
   
    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim frm As New NewKid
        'frm.ShowDialog()
        frm.eid = eid
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loadchild()
        End If
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim frm As New NewKid
        'frm.ShowDialog()
        frm.eid = eid
        Dim row = DataGridView1.SelectedRows(0)
        frm.txtname.Text = row.Cells("kName").Value
        frm.dtpdob.Value = row.Cells("kdob").Value
        frm.sid = row.Cells("SexId").Value
        frm.cid = row.Cells("ChildId").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loadchild()
        End If
    End Sub

    Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = DataGridView1.SelectedRows(0)
        'cmdp.Parameters("@id") = row.Cells("ChildId").Value
        'MessageBox.Show(row.Cells("ChildId").Value)
        
        cmdp.ExecuteNonQuery("UPDATE [Relative] SET Active=false WHERE RelateId=" & row.Cells("ChildId").Value & "")


        Dim r As DataGridViewRow
        r = DataGridView1.SelectedRows(0)
        Me.DataGridView1.Rows.Remove(r)
    End Sub
End Class